package com.zenika.zenbinding.client.validator;

import com.zenika.zenbinding.client.exception.DataMessage;
import com.zenika.zenbinding.client.exception.ValidatorException;

public class MaxValidator implements Validator {

	private String max;
	private DataMessage message;

	public MaxValidator(String max) {
		this.max = max;
		this.message = new DataMessage("Value entered must be less than " + max);
	}

	public void validate(Object object) throws ValidatorException {
		String type = object.getClass().getName();
		if (type.equals("java.lang.Integer")) {
			this.validateInteger((Integer) object);
		} else if (type.equals("java.lang.Double")) {
			this.validateDouble((Double) object);
		} else if (type.equals("java.lang.Float")) {
			this.validateFloat((Float) object);
		} else if (type.equals("java.lang.Short")) {
			this.validateShort((Short) object);
		} else if (type.equals("java.lang.Byte")) {
			this.validateByte((Byte) object);
		} else if (type.equals("java.lang.Long")) {
			this.validateLong((Long) object);
		} else {
			throw new ClassCastException("This validator only works for numeric type objects");
		}
	}

	public void validateInteger(Integer value) throws ValidatorException {
		Integer maxInteger = new Integer(this.max);
		int compare = maxInteger.compareTo(value);
		if (compare < 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateDouble(Double value) throws ValidatorException {
		Double maxDouble = new Double(this.max);
		int compare = maxDouble.compareTo(value);
		if (compare < 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateFloat(Float value) throws ValidatorException {
		Float maxFloat = new Float(this.max);
		int compare = maxFloat.compareTo(value);
		if (compare < 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateShort(Short value) throws ValidatorException {
		Short maxShort = new Short(this.max);
		int compare = maxShort.compareTo(value);
		if (compare < 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateByte(Byte value) throws ValidatorException {
		Byte maxByte = new Byte(this.max);
		int compare = maxByte.compareTo(value);
		if (compare < 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateLong(Long value) throws ValidatorException {
		Long maxLong = new Long(this.max);
		int compare = maxLong.compareTo(value);
		if (compare < 0) {
			throw new ValidatorException(this.message);
		}
	}

}
